#pragma once
#include "util.hpp"

// 单例

namespace cloud
{
    #define CONFIG_FILE "cloud.conf"
    class config
    {
    public:
        static config* get_instance()
        {
            static config c;
            return &c;
        }

        int get_hot_time() { return _hot_time; }
        uint16_t get_server_port() { return _server_port; }
        std::string get_server_ip() { return _server_ip; }
        std::string get_download_prefix() { return _download_prefix; }
        std::string get_packfile_suffix() { return _packfile_suffix; }
        std::string get_pack_dir() { return _pack_dir; }
        std::string get_back_dir() { return _back_dir; }
        std::string get_backup_file() { return _backup_file; } 

    private:
        config(){read_config();}
        config(const config&) = delete;

        void read_config()
        {
            file_util fu(CONFIG_FILE);
            std::string conf;
            fu.get_content(conf);

            Json::Value root;
            json_util::deserialize(root, conf);
            _hot_time = root["hot_time"].asInt();
            _server_port = root["server_port"].asUInt();
            _server_ip = root["server_ip"].asString();
            _download_prefix = root["download_prefix"].asString();
            _packfile_suffix = root["packfile_suffix"].asString();
            _pack_dir = root["pack_dir"].asString();
            _back_dir = root["back_dir"].asString();
            _backup_file = root["backup_file"].asString();

            INFO("配置文件读取完毕");
            // std::cout << "配置文件读取完毕" << std::endl;
        }

    private:
        int _hot_time;                 // 热点文件判断时间
        uint16_t _server_port;         // 服务器端口
        std::string _server_ip;        // 服务器ip
        std::string _download_prefix;  // 文件下载前缀路径
        std::string _packfile_suffix;  // 压缩包后缀
        std::string _pack_dir;         // 压缩文件存放路径
        std::string _back_dir;         // 热点文件存放路径
        std::string _backup_file;      // 服务端备份信息存放文件
    };
}